<?php 
 // if(!define('BASEPATH')) exit('No direct script access allowed');
 
 class Admin extends CI_Controller 
 {
	public function __construct()
	{
		parent::__construct();		

		//goi model san pham, danh muc, quang cao
		$this->load->model('Category');
		$this->load->model('Product');	
		$this->load->model('Ad');
		$this->load->model('Position');
		$this->load->model('Setting');
		$this->load->model('News');
		$this->load->model('NewsCategory');
		$this->load->model('Page');
		$this->load->model('Support');
		$this->load->model('User');
		//phân trang
		$this->load->library('pagination');
	}
	
	function showHeader()
	{
		//check login first
		$logged_in = $this->session->userdata('logged_in');
		if(isset($logged_in) && !empty($logged_in))
		{
			$session_data = $this->session->userdata('logged_in');
		    $data['username'] = $session_data['username'];   
		    $this->load->view('admin/header');
		}
		else
		{
			redirect('admin/show_login');			
		}
	}

	public function index()
	{
		$this->showHeader();
		//lay thong tin roi show ra day
		$toppage_data = $this->getAdminTopPageData();
		$this->load->view('admin/toppage',$toppage_data);
		//ket thuc body
		$this->load->view('admin/footer');
	}

	/************************************************************************
	 *
	 * Product
	 * Product section for Admin 
	 *
	 * Function products
	 * Function newProduct
	 * Function editProduct
	 * 
	************************************************************************/
	
	public function products($num_of_products = false)
	{
		$this->showHeader();
		//lay danh sach san pham show ra
		$data['categories'] = $this->Category->getAllCategories();
		//display products
		$data['products'] = $this->Product->getAllProducts($num_of_products);
		//paging products
		$config['base_url'] = site_url('admin/products');
		$config['total_rows'] = 200;
		$config['per_page'] = 5; 
		$this->pagination->initialize($config); 

		$data['products_paging'] = $this->pagination->create_links();
		//end paging
		$this->load->view('admin/products',$data);
		//lay danh sach danh muc show ra
		$this->load->view('admin/footer');
	}

	public function newProduct()
	{		
		//if form is submitted then redirect to homepage
		if(isset($_POST['submitBtn']) && !empty($_POST['submitBtn']))
		{						
			//upload image 
			//create thumb 40x40
			//create thumbnail 120x120
			//create image 250x250
			//if susccess return image and thumbnail
			$data = $this->getProductImage2('image');

			$thumb = $data['resize']['thumb'];
			$image = $data['resize']['image'];	
			$thumbnail = $data['resize']['thumbnail'];

			//adding more image
			$image1 = $this->getProductImage('image1',1);
			$image2 = $this->getProductImage('image2',1);
			$image3 = $this->getProductImage('image3',1);
			$image4 = $this->getProductImage('image4',1);
			//price
			//add fix patch for converting price to VND
			$price_origin = $this->input->post('price');
			$price = number_format($price_origin,0,",",".");
			//date added
			$created_date = date('d/m/Y',strtotime('now'));				

			$data = array(
					'name' 	         => $this->input->post('name'),
					'desc'			 => $this->input->post('desc'),
					'price'          => $price,
					'price_add'		 => $price_origin,
					'thumb'			 => $thumb,
					'thumbnail'		 => $thumbnail,
					'image'			 => $image,
					//further settings					
					'quantity' 	     => $this->getProductFieldValue(false,'quantity'),
					'origin'		 => $this->input->post('origin'),
					'warranty'		 => $this->input->post('warranty'),
					'status'		 => $this->input->post('status'),
					'modified_date'	 => $created_date,
					'created_date'	 => $created_date,
					'detail'		 => $this->input->post('detail'),
					'product_relate' => $this->input->post('product_relate'),
					'category_id'	 => $this->input->post('category_id'),
					//adding image
					'image1'		 => $image1['upload_data']['file_name'],
					'image2'		 => $image2['upload_data']['file_name'],
					'image3'		 => $image3['upload_data']['file_name'],
					'image4'		 => $image4['upload_data']['file_name'],	
					//adding thumb image 
					'image1_thumb'	 => $this->createThumbImage($image1),
					'image2_thumb'	 => $this->createThumbImage($image2),
					'image3_thumb'	 => $this->createThumbImage($image3),
					'image4_thumb'	 => $this->createThumbImage($image4),
				);			
			
			//add 6 product for category
			$i = 1;
			//while($i < 10)
			//{
				$this->Product->addNewProduct($data);
			//	$i++;	
			//}
			
			redirect('admin/products','refresh');			
		}
		else
		{			
			$this->showHeader();
			$data['categories'] = $this->Category->getAllCategories();
			$this->load->view('admin/newproduct',$data);
			$this->load->view('admin/footer');
		}				
	}

	public function resizeImage($path = false)
	{
		//load library
		$this->load->library('image_lib');

		//auto resize 120px x 120px
		$config['image_library'] = 'gd2';
		$config['source_image']	= $path;
		$config['create_thumb'] = TRUE;
		$config['maintain_ratio'] = TRUE;
		$config['width']	 = 120;
		$config['height']	= 120;

		$this->load->library('image_lib', $config); 

		$this->image_lib->resize();
	}

	public function editProduct($id)
	{
		//if id is empty then redirect to product page
		if($id == FALSE)
		{
			redirect('admin/index','refresh');
		}
		else
		{
			$product = $this->Product->getProduct($id);
			if(!isset($product) || empty($product))
			{
				redirect('admin/index','refresh');
			}			
		}

		//if submit button is pressed, then process updating product
		if(isset($_POST['submitBtn']) && !empty($_POST['submitBtn']) )
		{
			//thumbnail
			$thumbnail_upload = $this->getProductImage('thumbnail');
			//process image first
			$image_upload_result = $this->getProductImage();
			//image 1
			$image1_upload = $this->getProductImage('image1');
			//image 2
			$image2_upload = $this->getProductImage('image2');
			//image 3
			$image3_upload = $this->getProductImage('image3');
			//image 4
			$image4_upload = $this->getProductImage('image4');

			//manually
			if(!isset($image_upload_result['error']) && empty($image_upload_result['error']))
			{
				if(isset($product['image']) && !empty($product['image']))
				{
					$image_field_value = $product['image'];
				}
				else
				{
					$image_field_value = $image_upload_result['upload_data']['file_name'];
				}				
			}
			else
			{
				$image_field_value = $product['image'];
			}
			//thumbnail
			if(!isset($thumbnail_upload['error']) && empty($thumbnail_upload['error']))
			{
				if(isset($product['thumbnail']) && !empty($product['thumbnail']))
				{
					$thumbnail = $product['thumbnail'];
				}
				else
				{
					$thumbnail = $thumbnail_upload['upload_data']['file_name'];
				}					
			}
			else
			{
				$thumbnail = $product['thumbnail'];
			}
			//image 1
			if(!isset($image1_upload['error']) && empty($image1_upload['error']))
			{
				if(isset($product['image1']) && !empty($product['image1']))
				{
					$image1 = $product['image1'];
				}
				else
				{
					$image1 = $image1_upload['upload_data']['file_name'];
				}					
			}
			else
			{
				$image1 = $product['image1'];
			}
			//image 2
			if(!isset($image2_upload['error']) && empty($image2_upload['error']))
			{
				if(isset($product['image2']) && !empty($product['image2']))
				{
					$image2 = $product['image2'];
				}
				else
				{
					$image2 = $image2_upload['upload_data']['file_name'];
				}					
			}
			else
			{
				$image2 = $product['image2'];
			}
			//image 3
			if(!isset($image3_upload['error']) && empty($image3_upload['error']))
			{
				/*
				if(isset($product['image3']) && !empty($product['image3']))
				{
					$image3 = $product['image3'];
				}
				*/
				//else
				{
					$image3 = $image3_upload['upload_data']['file_name'];
				}					
			}
			else
			{
				$image3 = $product['image3'];
			}
			//image 4
			if(!isset($image4_upload['error']) && empty($image4_upload['error']))
			{				
				/*
				if(isset($product['image4']) && !empty($product['image4']))
				{
					$image4 = $product['image4'];
				}
				*/
				//else
				{
					$image4 = $image4_upload['upload_data']['file_name'];
				}					
			}
			else
			{
				$image4 = $product['image4'];
			}

			//price
			$origin_price = $this->input->post('price');
			$price_input = number_format($price_origin,0,",",".");

			$data = array(
					'name' 	         => $this->getProductFieldValue($product,'name'),
					'desc'			 =>	$this->getProductFieldValue($product,'desc'),
					'price'          => $price_input,
					'price_add'		 => $origin_price,
					'thumb'			 => $this->createThumbImage($image_upload_result),
					'thumbnail'		 => $thumbnail,
					'image'			 => $image_field_value,
					//further settings					
					'quantity' 	     => $this->getProductFieldValue($product,'quantity'),
					'origin'		 => $this->getProductFieldValue($product,'origin'),
					'warranty'		 => $this->getProductFieldValue($product,'warranty'),
					'status'		 => $this->getProductFieldValue($product,'status'),
					'modified_date'	 => date('d/m/Y',strtotime('now')),					
					'detail'		 => $this->getProductFieldValue($product,'detail'),
					'product_relate' => $this->getProductFieldValue($product,'product_relate'),
					'category_id'	 => $this->getProductFieldValue($product,'category_id'),
					//adding image
					'image1'		 => $image1,
					'image2'		 => $image2,
					'image3'		 => $image3,
					'image4'		 => $image4,	
					//thumb image
					'image1_thumb'	 => $this->createThumbImage($image1_upload),
					'image2_thumb'	 => $this->createThumbImage($image2_upload),
					'image3_thumb'	 => $this->createThumbImage($image3_upload),
					'image4_thumb'	 => $this->createThumbImage($image4_upload),
				);	
			$this->Product->editProduct($data,$id);
			redirect('admin/products','refresh');
		}	
		//otherwise show product need to edit					
		else
		{
			$data['categories'] = $this->Category->getAllCategories();
			$data['product'] = $product;
			$this->showHeader();
			$this->load->view('admin/editproduct',$data);
			$this->load->view('admin/footer');
		}
	}

	function getProductImage($imageField = false,$debug = false)
	{
		if($imageField == false)
		{
			$imageField = 'image';
		}
		//if product image is uploaded, then replace current image with new image
		$input_image = $this->input->post($imageField);

		if(!isset($input_image) && empty($input_image))
		{
			$data['upload_data']['file_name'] = 'no_image.jpg';
		}
		else
		{
			$config['upload_path'] = './upload/image/products';
			$config['allowed_types'] = 'gif|jpg|png';
			$config['max_size']	= '500';
			$config['max_width']  = '1024';
			$config['max_height']  = '768';

			$this->load->library('upload', $config);

			if ( ! $this->upload->do_upload($imageField))
			{
				//$data = array('error' => $this->upload->display_errors());	
				$data['upload_data']['file_name'] = 'no_image.jpg';			
			}
			else
			{
				/*
				$file_name = $data['upload_data']['raw_name'];
				$file_type = $data['upload_data']['file_ext'];
				$type = 'origin';
				$this->resize_image($file_name,$file_type,$type);
				*/
				$data = array('upload_data' => $this->upload->data());			
			}
		}				
		
		return $data;
	}

	function createThumbImage($data = false)
	{
		if($data == false)
		{
			redirect('admin/products','refresh');			
		}

		if($data['upload_data']['file_name'] == 'no_image.jpg')
		{
			return 'thum_no_image.jpg';
		}
		else
		{
			//resize with image name + '40x40' + '.jpg'
			$file_name = $data['upload_data']['raw_name'];
			$file_type = $data['upload_data']['file_ext'];
			$type 	   = 'thumb';
			$this->resize_image($file_name,$file_type,$type);
			return $file_name.'40x40'.$file_type;
		}
	}

	function getProductImage2($imageField = false,$debug = false)
	{
		if($imageField == false)
		{
			$imageField = 'image';
		}
		//if product image is uploaded, then replace current image with new image
		$input_image = $this->input->post($imageField);

		if(!isset($input_image) && empty($input_image))
		{
			$data['resize']['image'] = 'no_image.jpg';
			$data['resize']['thumbnail'] = 'thumbnail_no_image.jpg';
			$data['resize']['thumb'] 	= 'thum_no_image.jpg';
		}
		else
		{
			$config['upload_path'] = './upload/image/products';
			$config['allowed_types'] = 'gif|jpg|png';
			$config['max_size']	= '500';
			$config['max_width']  = '1024';
			$config['max_height']  = '768';

			$this->load->library('upload', $config);

			if ( ! $this->upload->do_upload($imageField))
			{
				//$data = array('error' => $this->upload->display_errors());	
				$data['resize']['image'] = 'no_image.jpg';
				$data['resize']['thumbnail'] = 'thumbnail_no_image.jpg';
				$data['resize']['thumb'] 	= 'thum_no_image.jpg';
			}
			else
			{
				$data = array('upload_data' => $this->upload->data());	

				//creat thumb 24x24
				$this->resize_image($data['upload_data']['raw_name'],$data['upload_data']['file_ext'],'thumb');
				//create thumbnail 120x120px				
				$this->resize_image($data['upload_data']['raw_name'],$data['upload_data']['file_ext'],'thumbnail');
				//create image 250x250px
				//return 
				$this->resize_image($data['upload_data']['raw_name'],$data['upload_data']['file_ext'],'image');
					
				$data = array(
					'resize' => array(
							'thumb' => $data['upload_data']['raw_name'].'40x40'.$data['upload_data']['file_ext'],
							'image' => $data['upload_data']['raw_name'].'250x250'.$data['upload_data']['file_ext'],
							'thumbnail' => $data['upload_data']['raw_name'].'120x120'.$data['upload_data']['file_ext'],
						)
					);
			}
		}				
		
		return $data;
	}


	function resize_image($file_name = false,$file_type = false,$type = false)
	{
		$config['image_library'] = 'gd2';
		$config['source_image']	= './upload/image/products/'.$file_name.$file_type;
		if($type == 'thumb')
		{
			$config['new_image']    = './upload/image/products/'.$file_name.'40x40'.$file_type;
		}
		if($type == 'thumbnail')
		{
			$config['new_image']    = './upload/image/products/'.$file_name.'120x120'.$file_type;
		}
		else if($type == 'image')
		{
			$config['new_image']    = './upload/image/products/'.$file_name.'250x250'.$file_type;
		}		
		else if($type == 'origin')
		{
			$config['new_image']    = './upload/image/products/'.$file_name.$file_type;
		}
		$config['create_thumb'] = FALSE;
		$config['maintain_ratio'] = TRUE;
		if($type == 'thumb')
		{
			$config['width']	 = 40;
			$config['height']	= 40;
		}
		if($type == 'thumbnail')
		{
			$config['width']	 = 120;
			$config['height']	= 120;
		}
		else if($type == 'image')
		{
			$config['width']	 = 420;
			$config['height']	= 420;
		}
		else if($type == 'origin')
		{
			$config['width']	 = 400;
			$config['height']	= 420;	
		}		

		$this->load->library('image_lib', $config);

		//path fix add multiple images 
		$this->image_lib->clear();
		$this->image_lib->initialize($config); 

		$this->image_lib->resize();
	}

	function getProductFieldValue($productData = false,$fieldName = false,$id = false)
	{
		$fieldValue = $this->input->post($fieldName);		
		if(isset($fieldValue) && !empty($fieldValue))
		{
			return $fieldValue;
		}
		else
		{			
			if($productData != false)
			{
				return $productData[$fieldName];
			}
			else
			{
				return 1;	
			}				
		}
	}

	/************************************************************************
	 * End Product section
	************************************************************************/
		
	/************************************************************************
	 *
	 * Category
	 * Category section for Admin 
	 *
	 * Function newCategory
	 * add new category
	 * Function editCategory
	 * 
	************************************************************************/
		
	//add new category	
	public function newCategory()
	{		
		//if form is submitted then redirect to homepage
		if(isset($_POST['submitBtn']) && !empty($_POST['submitBtn']))
		{			
			$data = array(
					'category' 	   => $this->input->post('category'),
					'sub_category' => $this->input->post('sub_category'),
					'active'	   => 1,
				);			
			$this->Category->addCategory($data);
			redirect('admin/newCategory','refresh');
		}
		else
		{			
			$this->showHeader();
			$data['categories'] = $this->Category->getAllCategories1();
			$this->load->view('admin/newcategory',$data);
			$this->load->view('admin/footer');
		}		
	}

	//edit category
	public function editCategory($id = false)
	{		
		if($id == false)
		{
			redirect('admin/products','refresh');
		}
		else
		{
			$category = $this->Category->getCategory($id);


			if(isset($category) && !empty($category))
			{
				//edit button is pressed
				if(isset($_POST['submitBtn']) && !empty($_POST['submitBtn']))
				{
					$data = array(
							'category'		=>	$this->getCategoryFieldValue($category,'category'),
							'sub_category'	=>	$this->getCategoryFieldValue($category,'sub_category'),
						);
					$this->Category->updateCategory($data,$id);
					redirect('admin/products','refresh');
				}	
				else
				{
					$data['edit_category'] = $category;
					$data['categories'] = $this->Category->getAllCategories();
					$this->showHeader();
					$this->load->view('admin/editcategory',$data);
					$this->load->view('admin/footer');
				}			
			}
			else
			{
				redirect('admin/products','refresh');
			}
		}
	}

	//show products of category
	public function showProductsOfCategory($id = false,$num_of_products = false)
	{		
		//if id is not defined, then redirect to homepage
		if($id == false)
		{
			//error notify 
			redirect('admin/index','refresh');
		}
		//products
		$data['products'] = $this->Category->showProductsOfCategory($id,$num_of_products);
		//paging products
		$config['base_url'] = site_url('admin/showProductsOfCategory/'.$id.'/');
		$config['total_rows'] = 200;
		$config['per_page'] = 5; 

		$this->pagination->initialize($config); 
		$data['products_paging'] = $this->pagination->create_links();
		//end paging products
		$data['categories'] = $this->Category->getAllCategories();

		$this->showHeader();
		$this->load->view('admin/products',$data);
		$this->load->view('admin/footer');
	}

	function getCategoryFieldValue($categoryData = false,$fieldName = false,$id = false)
	{
		$fieldValue = $this->input->post($fieldName);		
		if(isset($fieldValue) && !empty($fieldValue))
		{
			return $fieldValue;
		}
		else
		{			
			if($categoryData != false)
			{
				return $categoryData[$fieldName];
			}
			else
			{
				return 1;	
			}				
		}
	}

	/************************************************************************
	 * End Category section
	************************************************************************/

	/************************************************************************
	 * Advertisement section
	************************************************************************/

	public function ads($pos = false)
	{
		if($pos == 'left')
		{
			$data['ads'] = $this->Ad->getLeftAds();
		}
		else if($pos == 'right')
		{
			$data['ads'] = $this->Ad->getRightAds();
		}
		else if($pos == 'home')
		{
			$data['ads'] = $this->Ad->getHomeAds();
		}
		else
		{
			$data['ads'] = $this->Ad->getAllAds();			
		}	
		
		$this->showHeader();
		$this->load->view('admin/ads',$data);
		$this->load->view('admin/footer');		
	}

	function addNewAd()
	{
		//if Ad is processed then we do submit
		if(isset($_POST['submitBtn']) && !empty($_POST['submitBtn']))
		{
			$image_data = $this->getAdImage();
			$image_input = $image_data['upload_data']['file_name'];
			$data = array(
					'title' => $this->getAdFieldValue('title'),
					'href'	=> $this->getAdFieldValue('href'),
					'image' => $image_input,
					'order' => $this->getAdFieldValue('order'),
					'position_id' => $this->getAdFieldValue('position_id'),
				);
			$this->Ad->addNewAd($data);
			redirect('admin/ads','refresh');
		}
		else
		{
			$data['positions'] = $this->Position->getAll();
			$this->showHeader();
			$this->load->view('admin/addNewAd',$data);
			$this->load->view('admin/footer');
		}
		
	}

	function editAd($id = false)
	{
		if($id == false)
		{
			redirect('admin/ads','refresh');			
		}

		$ad = $this->Ad->getAd($id);

		if(isset($_POST['submitBtn']) && !empty($_POST['submitBtn']))
		{
			$image = $this->getAdImage();
			if(isset($image['error']) && !empty($image['error']))
			{
				$image_input = $ad['image'];
			}
			else
			{
				$image_input = $image['upload_data']['file_name'];
			}
			$data = array(
					'title' => $this->getAdFieldValue('title',$ad),
					'href'	=> $this->getAdFieldValue('href',$ad),
					'image' => $image_input,
					'order' => $this->getAdFieldValue('order',$ad),
					'position_id' => $this->getAdFieldValue('position_id',$ad),
				);

			$this->Ad->updateAd($data,$id);
			redirect('admin/ads','refresh');
		}
		else
		{
			$data['ad'] = $ad;
			$data['positions'] = $this->Position->getAll();
			$this->showHeader();
			$this->load->view('admin/editAd',$data);
			$this->load->view('admin/footer');
		}
	}

	function getAdFieldValue($fieldName = false, $adData = false)
	{
		$fieldValue = $this->input->post($fieldName);
		if(isset($fieldValue) && !empty($fieldValue))
		{
			return $fieldValue;
		}
		else
		{
			if($adData)
			{
				return $adData[$fieldName];
			}
			else
			{
				return 1;
			}
		}
	}



	function getAdImage($id = false)
	{
		//if product image is uploaded, then replace current image with new image
		$input_image = $this->input->post('image');

		if(!isset($input_image) && empty($input_image))
		{
			die('empty image');
		}
				
		$config['upload_path'] = './upload/image/ads';
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size']	= '500';
		$config['max_width']  = '1024';
		$config['max_height']  = '768';

		$this->load->library('upload', $config);

		if ( ! $this->upload->do_upload('image'))
		{
			$data = array('error' => $this->upload->display_errors());				
		}
		else
		{
			$data = array('upload_data' => $this->upload->data());			
		}
		return $data;
	}

	/************************************************************************
	 * End Advertisement section
	************************************************************************/

	/************************************************************************
	 * Settings section
	************************************************************************/

	public function settings()
	{
		$data['settings'] = $this->Setting->getCompany();
		$this->showHeader();
		$this->load->view('admin/settings',$data);
		$this->load->view('admin/footer');
	}

	function editSetting($id)
	{
		if($id == false)
		{
			redirect('admin/products','refresh');
		}

		$data['settingData'] = $this->Setting->getCompany($id);

		if(isset($_POST['submitBtn']) && !empty($_POST['submitBtn']))
		{
			$image = $this->getSettingImage();

			if(isset($image['error']) && !empty($image['error']))
			{
				$image_input = $data['settingData']['map'];
			}
			else
			{
				$image_input = $image['upload_data']['file_name'];
			}
			

			$data = array(
					'company' => $this->getSettingFieldValue('company',$data['settingData']),
					'website' => $this->getSettingFieldValue('website',$data['settingData']),
					'description' => $this->getSettingFieldValue('description',$data['settingData']),
					'address' => $this->getSettingFieldValue('address',$data['settingData']),
					'contact' => $this->getSettingFieldValue('contact',$data['settingData']),
					'email'  => $this->getSettingFieldValue('email',$data['settingData']),
					'map' => $image_input,
				);
			$this->Setting->updateSetting($data,$id);
			redirect('admin/settings','refresh');
		}
		else
		{
			$this->showHeader();
			$this->load->view('admin/editSetting',$data);
			$this->load->view('admin/footer');
		}
	}

	function getSettingFieldValue($fieldName = false, $settingData = false)
	{
		$fieldValue = $this->input->post($fieldName);
		if(isset($fieldValue) && !empty($fieldValue))
		{
			return $fieldValue;
		}
		else
		{
			if($settingData)
			{
				return $settingData[$fieldName];
			}
			else
			{
				return 1;
			}
		}
	}

	function getSettingImage($id = false)
	{
		//if product image is uploaded, then replace current image with new image
		$input_image = $this->input->post('image');

		if(!isset($input_image) && empty($input_image))
		{
			die('empty image');
		}
				
		$config['upload_path'] = './upload/image/settings';
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size']	= '500';
		$config['max_width']  = '1024';
		$config['max_height']  = '768';

		$this->load->library('upload', $config);

		if ( ! $this->upload->do_upload('image'))
		{
			$data = array('error' => $this->upload->display_errors());				
		}
		else
		{
			$data = array('upload_data' => $this->upload->data());			
		}
		return $data;
	}

	/************************************************************************
	 * End Settings section
	************************************************************************/

	/************************************************************************
	 * News section
	************************************************************************/

	public function news()
	{
		$data['news'] = $this->News->getAll();
		$data['categories']	= $this->NewsCategory->getAll();

		$this->showHeader();
		$this->load->view('admin/news',$data);
		$this->load->view('admin/footer');		
	}

	public function addNewCategory()
	{
		if(isset($_POST['submitBtn']) && !empty($_POST['submitBtn']))
		{
			$data = array(
					'category' => $this->getNewsFieldValue('category'),
					'sub_category' => $this->getNewsFieldValue('sub_category'),
				);
			$this->NewsCategory->addNewCategory($data);
			redirect('admin/news','refresh');
		}
		else
		{
			$this->showHeader();
			$this->load->view('admin/addnewscategory');
			$this->load->view('admin/footer');
		}
	}

	public function addNew()
	{
		if(isset($_POST['submitBtn']) && !empty($_POST['submitBtn']))
		{
			$image = $this->getNewImage();
			$data = array(
					'title' => $this->getNewsFieldValue('title'),
					'image' => $image['upload_data']['file_name'],
					'description' => $this->getNewsFieldValue('description'),
					'category_id' => $this->getNewsFieldValue('category_id'),
				);
			$this->News->addNew($data);
			redirect('admin/news','refresh');
		}
		else
		{
			$data['categories'] = $this->NewsCategory->getAll();
			$this->showHeader();
			$this->load->view('admin/addNews',$data);
			$this->load->view('admin/footer');
		}
	}

	function getNewsFieldValue($fieldName = false, $newsData = false)
	{
		$fieldValue = $this->input->post($fieldName);
		if(isset($fieldValue) && !empty($fieldValue))
		{
			return $fieldValue;
		}
		else
		{
			if($newsData)
			{
				return $newsData[$fieldName];
			}
			else
			{
				return 1;
			}
		}
	}

	function getNewImage($id = false)
	{
		//if product image is uploaded, then replace current image with new image
		$input_image = $this->input->post('image');

		if(!isset($input_image) && empty($input_image))
		{
			die('empty image');
		}
				
		$config['upload_path'] = './upload/image/news';
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size']	= '500';
		$config['max_width']  = '1024';
		$config['max_height']  = '768';

		$this->load->library('upload', $config);

		if ( ! $this->upload->do_upload('image'))
		{
			$data = array('error' => $this->upload->display_errors());				
		}
		else
		{
			$data = array('upload_data' => $this->upload->data());			
		}
		return $data;
	}

	/************************************************************************
	 * Order Section
	 * temporarily disable section
	***********************************************************************/
	public function orders()
	{				
		redirect('admin/products','refresh');
	}

	/************************************************************************
	 * End Order Section
	 * temporarily disable section
	***********************************************************************/

	/************************************************************************
	 * Pages Section	
	***********************************************************************/

	public function pages()
	{
		$this->showHeader();
		$data['pages'] = $this->Page->getAll();
		$this->load->view('admin/pages',$data);
		$this->load->view('admin/footer');
	}

	function addNewPage()
	{
		if(isset($_POST['submitBtn']) && !empty($_POST['submitBtn']))
		{
			$data = array(
					'name' => $this->getPageFieldValue('name'),
					'title' => $this->getPageFieldValue('title'),
					'content' => $this->getPageFieldValue('content'),
					'active' => 1,
				);
			$this->Page->addNewPage($data);
			redirect('admin/pages','refresh');
		}
		else
		{
			$this->showHeader();			
			$this->load->view('admin/addNewPage');
			$this->load->view('admin/footer');
		}
	}

	function editPage($id = false)
	{
		if($id == false)
			redirect('admin/pages','refresh');

		$page_data = $this->Page->getPage($id);

		if(isset($_POST['submitBtn']) && !empty($_POST['submitBtn']))
		{
			$data = array(
					'name' => $this->getPageFieldValue('name',$page_data),
					'title' => $this->getPageFieldValue('title',$page_data),
					'content' => $this->getPageFieldValue('content',$page_data),
					'active' => 1,
				);
			$this->Page->updatePage($data,$id);
			redirect('admin/pages','refresh');
		}
		else
		{
			$data['page'] = $page_data;
			$this->showHeader();
			$this->load->view('admin/editPage',$data);
			$this->load->view('admin/footer');
		}
	}

	function getPageFieldValue($fieldName = false, $pagesData = false)
	{
		$fieldValue = $this->input->post($fieldName);
		if(isset($fieldValue) && !empty($fieldValue))
		{
			return $fieldValue;
		}
		else
		{
			if($pagesData)
			{
				return $pagesData[$fieldName];
			}
			else
			{
				return 1;
			}
		}
	}

	/************************************************************************
	 * End Pages Section	
	***********************************************************************/

	/************************************************************************
	 * Login Section	
	***********************************************************************/

	function show_login()
	{
		$this->load->view('admin/login_view','refresh');
	}

	function login()
	{
		$this->load->model('User');
		//load library form validation
     	$this->load->library('form_validation');
     	//
     	$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
     	$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database');

     	if($this->form_validation->run() == FALSE)
     	{
     		//Field validation failed.  User redirected to login page
     		$this->load->view('admin/login_view');
     	}
     	else
     	{
     		//Go to private area
     		redirect('admin/index', 'refresh');
     	}
	}

	function logout()
	{
		 $this->session->unset_userdata('logged_in');
		 session_destroy();
		 redirect('admin/show_login', 'refresh');
	}


 	function check_database($password)
	 {
	   //Field validation succeeded.  Validate against database
	   $username = $this->input->post('username');

	   //query the database
	   $result = $this->User->login($username, $password);

	   if($result)
	   {
	     $sess_array = array();
	     foreach($result as $row)
	     {
	       $sess_array = array(
	         'id' => $row->id,
	         'username' => $row->username
	       );
	       $this->session->set_userdata('logged_in', $sess_array);
	     }
	     return TRUE;
	   }
	   else
	   {
	     $this->form_validation->set_message('check_database', 'Invalid username or password');
	     return false;
	   }
	 }

	/************************************************************************
	 * End Login Section	
	***********************************************************************/

	/************************************************************************
	 * End News section
	************************************************************************/

	function getAdminTopPageData()
	{
		$toppage_data = array();
		$toppage_data['num_of_products'] = 10;
		$toppage_data['message'] = 10;
		$toppage_data['stat_count'] = 10000;			
		return $toppage_data;
	}
	
	function onlinesupport()
	{
		$data['support'] = $this->Support->getAll();
		$this->showHeader();
		$this->load->view('admin/onlinesupport',$data);
		$this->load->view('admin/footer');
	}	

	function newsupport()
	{		
		$this->showHeader();
		$this->load->view('admin/onlinesupport',$data);
		$this->load->view('admin/footer');
	}


 }